package com.ideal.blockchain.controller.block;

import com.alibaba.fastjson.JSONObject;
import com.ideal.blockchain.config.ChannelContext;
import com.ideal.blockchain.dto.request.BlockDto;
import com.ideal.blockchain.dto.response.ResultInfo;
import com.ideal.blockchain.enums.ResponseCodeEnum;
import com.ideal.blockchain.service.block.BlockService;
import com.ideal.blockchain.service.block.UserService;
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.fabric.sdk.BlockInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: Jackie
 * @date: 2021/01/10 12:04
 */
@Slf4j
@RestController
public class PeerController {
    @Autowired
    private BlockService blockService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/num", method = RequestMethod.POST)
    public ResultInfo getBlockInfo(@RequestBody BlockDto blockDto) throws Exception {
        try {
            if (StringUtils.isEmpty(blockDto.getUserName())) {
                return new ResultInfo(ResponseCodeEnum.FAILURE, "please enter username in request body!");
            }
//            if (StringUtils.isEmpty(blockDto.getPassWord())) {
//                return new ResultInfo(ResponseCodeEnum.FAILURE, "please enter passwords in request body");
//            }
            if (StringUtils.isEmpty(blockDto.getPeerWithOrg())) {
                return new ResultInfo(ResponseCodeEnum.FAILURE, "please enter peerWithOrg in request body");
            }
            if (StringUtils.isEmpty(blockDto.getChannelName())) {
                return new ResultInfo(ResponseCodeEnum.FAILURE, "please enter channelName in request body");
            }
            if (StringUtils.isEmpty(blockDto.getNumber())) {
                return new ResultInfo(ResponseCodeEnum.FAILURE, "please enter number in request body");
            }
            log.debug(blockDto.getUserName());

            String result = userService.loadUserFromPersistence(blockDto.getUserName(), blockDto.getPassWord(), blockDto.getPeerWithOrg());
            if (result == "Successfully loaded member from persistence") {
                BlockInfo blockInfo = blockService.blockchainInfo(blockDto.getUserName(), blockDto.getPeerWithOrg(), blockDto.getChannelName());

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("height",blockInfo.getBlockNumber());
                jsonObject.put("PreviousHash",blockInfo.getPreviousHash());
                return new ResultInfo(ResponseCodeEnum.SUCCESS, jsonObject.toJSONString());
            } else {
                return new ResultInfo(ResponseCodeEnum.FAILURE, "Something went wrong");
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return new ResultInfo(ResponseCodeEnum.FAILURE, e.getMessage());
        } finally {
            ChannelContext.clear();
        }
    }
}
